extends CanvasLayer
class_name GameHUD

# 游戏主HUD控制器
# 管理所有UI组件的显示和交互

# 游戏状态枚举
enum GameState {
	START_SCREEN,
	PLAYING,
	PAUSED,
	GAME_OVER,
	VICTORY  # 新增：通关状态
}

# UI组件引用
@onready var health_display: HealthDisplay = $HealthDisplay
@onready var item_inventory: ItemInventory = $ItemInventory
@onready var game_controls: GameControls = $GameControls
@onready var player_stats: PlayerStats = $PlayerStats

# 暂停界面组件
var pause_overlay: Control
var pause_panel: Panel
var pause_label: Label
var resume_instructions: Label
var pause_main_menu_button: Button

# 游戏结束界面组件
var game_over_overlay: Control
var game_over_panel: Panel
var game_over_label: Label
var death_message: Label
var restart_button: Button
var main_menu_button: Button

# 通关界面组件
var victory_overlay: Control
var victory_panel: Panel
var victory_label: Label
var victory_message: Label
var victory_restart_button: Button
var victory_main_menu_button: Button

# 游戏状态
var current_state: GameState = GameState.START_SCREEN
var game_paused: bool = false
var inventory_open: bool = false

signal pause_game_requested
signal resume_game_requested
signal menu_requested
signal start_game_requested
signal restart_game_requested
signal return_to_main_menu_requested
signal quit_game_requested

func _ready():
	print("GameHUD _ready() 开始初始化...")
	
	# 设置处理模式为始终处理 - 确保在暂停和非暂停状态下都能工作
	process_mode = Node.PROCESS_MODE_ALWAYS
	
	# 获取屏幕信息
	var viewport = get_viewport()
	var screen_size = viewport.get_visible_rect().size
	print("屏幕尺寸: ", screen_size)
	
	# 连接信号
	setup_signals()
	
	# 初始化UI组件
	initialize_components()
	
	# 设置开始界面状态
	set_start_screen_state()
	
	print("GameHUD 初始化完成，当前状态: ", current_state)

func _input(event):
	# 处理快捷键 - 在不同状态下有不同的行为
	if current_state == GameState.GAME_OVER or current_state == GameState.VICTORY:
		# 游戏结束状态和通关状态下不处理任何快捷键，只能通过按钮操作
		return
		
	if event.is_action_pressed("toggle_pause"):
		print("⌨️ 检测到ESC键按下，当前状态: ", current_state)
		# ESC键在游戏进行中和暂停状态下都可以切换暂停
		if current_state == GameState.PLAYING or current_state == GameState.PAUSED:
			toggle_pause()
		else:
			print("⚠️ 当前状态不允许切换暂停")
	elif event.is_action_pressed("toggle_inventory"):
		# 物品栏只在游戏进行中可以切换
		if current_state == GameState.PLAYING:
			toggle_inventory()
	elif event.is_action_pressed("ui_cancel"):
		print("⌨️ 检测到取消键按下，当前状态: ", current_state)
		# 取消键的优先级处理
		if current_state == GameState.PLAYING:
			if inventory_open:
				toggle_inventory()
		elif current_state == GameState.PAUSED:
			print("🔄 暂停状态下尝试恢复游戏")
			toggle_pause()  # 暂停状态下ESC恢复游戏

func setup_signals():
	# 连接游戏控制信号
	if game_controls:
		print("🔗 连接GameControls信号...")
		game_controls.pause_pressed.connect(_on_pause_pressed)
		game_controls.menu_pressed.connect(_on_menu_pressed)
		game_controls.start_game_pressed.connect(_on_start_game_pressed)
		game_controls.quit_game_pressed.connect(_on_quit_game_pressed)  # 连接退出游戏信号
		print("✅ GameControls信号连接完成")
	else:
		print("❌ GameControls节点未找到，无法连接信号")

func initialize_components():
	print("开始初始化UI组件...")
	
	# 确保所有UI组件都能在任何状态下工作
	for child in get_children():
		if child is Control:
			child.process_mode = Node.PROCESS_MODE_ALWAYS
	
	# 初始化各个UI组件
	if health_display:
		print("初始化HealthDisplay...")
		health_display.initialize()
	else:
		print("错误：HealthDisplay节点未找到！")
	
	if item_inventory:
		print("初始化ItemInventory...")
		item_inventory.initialize()
		item_inventory.visible = false
	else:
		print("错误：ItemInventory节点未找到！")
	
	if game_controls:
		print("初始化GameControls...")
		game_controls.initialize()
	else:
		print("错误：GameControls节点未找到！")
	
	if player_stats:
		print("初始化PlayerStats...")
		player_stats.initialize()
	else:
		print("错误：PlayerStats节点未找到！")
	
	# 创建暂停界面
	create_pause_overlay()
	
	# 创建游戏结束界面
	create_game_over_overlay()
	
	# 创建通关界面
	create_victory_overlay()
	
	# 确保GameControls在最上层（在所有界面之后）
	if game_controls:
		move_child(game_controls, -1)  # 移动到最后（最上层）
		print("🔝 GameControls移动到最上层")
	
	print("UI组件初始化完成")

func set_start_screen_state():
	print("🔄 设置开始界面状态...")
	
	# 设置开始界面状态
	current_state = GameState.START_SCREEN
	
	# 重置所有状态变量
	game_paused = false
	inventory_open = false
	
	# 隐藏游戏UI元素
	if health_display:
		health_display.visible = false
	
	if player_stats:
		player_stats.visible = false
	
	if item_inventory:
		item_inventory.visible = false
	
	# 隐藏所有覆盖界面
	hide_pause_overlay()
	hide_game_over()
	hide_victory()  # 隐藏通关界面
	
	# 确保控制按钮处于开始状态
	if game_controls:
		game_controls.reset_to_start_screen()
		# 确保暂停按钮状态被重置
		game_controls.set_pause_button_state(false)
	
	print("✅ 开始界面状态设置完成")

func start_game():
	print("🎮 开始游戏函数被调用")
	# 开始游戏
	current_state = GameState.PLAYING
	print("🔄 游戏状态切换为: ", current_state)
	
	# 显示游戏UI元素
	if health_display:
		health_display.visible = true
		print("👁️ 显示健康显示")
	
	if player_stats:
		player_stats.visible = true
		print("👁️ 显示玩家统计")
	
	# 发射开始游戏信号
	start_game_requested.emit()
	print("📤 发出start_game_requested信号")

func create_pause_overlay():
	# 创建暂停界面覆盖层
	pause_overlay = Control.new()
	add_child(pause_overlay)
	pause_overlay.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	pause_overlay.visible = false
	pause_overlay.mouse_filter = Control.MOUSE_FILTER_IGNORE  # 允许点击穿透到按钮
	# 确保暂停界面在任何状态下都能工作
	pause_overlay.process_mode = Node.PROCESS_MODE_ALWAYS
	
	# 创建半透明背景
	var background = ColorRect.new()
	pause_overlay.add_child(background)
	background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	background.color = Color(0, 0, 0, 0.5)  # 半透明黑色
	background.mouse_filter = Control.MOUSE_FILTER_IGNORE  # 背景也允许点击穿透
	
	# 创建暂停面板 - 增加高度以容纳返回主菜单按钮
	pause_panel = Panel.new()
	pause_overlay.add_child(pause_panel)
	pause_panel.size = Vector2(200, 160)  # 增加高度
	pause_panel.position = Vector2((get_viewport().get_visible_rect().size.x - 200) / 2, 
									(get_viewport().get_visible_rect().size.y - 160) / 2)
	pause_panel.add_theme_color_override("bg_color", Color(0.1, 0.1, 0.1, 0.9))
	pause_panel.mouse_filter = Control.MOUSE_FILTER_STOP  # 面板本身阻止点击穿透
	
	# 创建抗锯齿字体
	var clear_font = SystemFont.new()
	clear_font.font_names = ["Arial", "Microsoft YaHei", "SimSun"]
	clear_font.antialiasing = TextServer.FONT_ANTIALIASING_GRAY
	clear_font.hinting = TextServer.HINTING_LIGHT
	
	# 创建暂停标题
	pause_label = Label.new()
	pause_panel.add_child(pause_label)
	pause_label.text = "游戏已暂停"
	pause_label.position = Vector2(20, 20)
	pause_label.size = Vector2(160, 30)
	pause_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	pause_label.add_theme_color_override("font_color", Color.YELLOW)
	pause_label.add_theme_color_override("font_shadow_color", Color.BLACK)
	pause_label.add_theme_constant_override("shadow_offset_x", 2)
	pause_label.add_theme_constant_override("shadow_offset_y", 2)
	pause_label.add_theme_font_size_override("font_size", 16)
	pause_label.add_theme_font_override("font", clear_font)
	
	# 创建恢复游戏说明
	resume_instructions = Label.new()
	pause_panel.add_child(resume_instructions)
	resume_instructions.text = "按 ESC 键或点击继续按钮\n恢复游戏"
	resume_instructions.position = Vector2(20, 60)
	resume_instructions.size = Vector2(160, 40)
	resume_instructions.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	resume_instructions.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
	resume_instructions.add_theme_color_override("font_color", Color.WHITE)
	resume_instructions.add_theme_color_override("font_shadow_color", Color.BLACK)
	resume_instructions.add_theme_constant_override("shadow_offset_x", 1)
	resume_instructions.add_theme_constant_override("shadow_offset_y", 1)
	resume_instructions.add_theme_font_size_override("font_size", 10)
	resume_instructions.add_theme_font_override("font", clear_font)
	
	# 创建返回主菜单按钮（暂停界面）
	pause_main_menu_button = Button.new()
	pause_panel.add_child(pause_main_menu_button)
	pause_main_menu_button.text = "返回主菜单"
	pause_main_menu_button.position = Vector2(60, 120)  # 居中放置
	pause_main_menu_button.size = Vector2(80, 30)
	pause_main_menu_button.add_theme_color_override("font_color", Color.WHITE)
	pause_main_menu_button.add_theme_color_override("font_shadow_color", Color.BLACK)
	pause_main_menu_button.add_theme_font_size_override("font_size", 12)
	pause_main_menu_button.add_theme_font_override("font", clear_font)
	pause_main_menu_button.pressed.connect(_on_pause_main_menu_button_pressed)
	
	print("暂停界面创建完成")

func create_game_over_overlay():
	print("🔧 开始创建游戏结束界面...")
	
	# 如果已经存在，先清理
	if game_over_overlay:
		print("⚠️ 游戏结束界面已存在，先清理...")
		game_over_overlay.queue_free()
	
	# 创建游戏结束界面覆盖层
	game_over_overlay = Control.new()
	add_child(game_over_overlay)
	game_over_overlay.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	game_over_overlay.visible = false
	game_over_overlay.mouse_filter = Control.MOUSE_FILTER_STOP  # 阻止点击穿透
	game_over_overlay.process_mode = Node.PROCESS_MODE_ALWAYS
	
	# 创建半透明红色背景（死亡主题）
	var background = ColorRect.new()
	game_over_overlay.add_child(background)
	background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	background.color = Color(0.5, 0.1, 0.1, 0.7)  # 暗红色半透明
	
	# 创建游戏结束面板
	game_over_panel = Panel.new()
	game_over_overlay.add_child(game_over_panel)
	game_over_panel.size = Vector2(300, 200)
	game_over_panel.position = Vector2((get_viewport().get_visible_rect().size.x - 300) / 2, 
									  (get_viewport().get_visible_rect().size.y - 200) / 2)
	game_over_panel.add_theme_color_override("bg_color", Color(0.1, 0.1, 0.1, 0.95))
	
	# 创建抗锯齿字体
	var clear_font = SystemFont.new()
	clear_font.font_names = ["Arial", "Microsoft YaHei", "SimSun"]
	clear_font.antialiasing = TextServer.FONT_ANTIALIASING_GRAY
	clear_font.hinting = TextServer.HINTING_LIGHT
	
	# 创建游戏结束标题
	game_over_label = Label.new()
	game_over_panel.add_child(game_over_label)
	game_over_label.text = "游戏结束"
	game_over_label.position = Vector2(20, 20)
	game_over_label.size = Vector2(260, 40)
	game_over_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	game_over_label.add_theme_color_override("font_color", Color.RED)
	game_over_label.add_theme_color_override("font_shadow_color", Color.BLACK)
	game_over_label.add_theme_constant_override("shadow_offset_x", 2)
	game_over_label.add_theme_constant_override("shadow_offset_y", 2)
	game_over_label.add_theme_font_size_override("font_size", 20)
	game_over_label.add_theme_font_override("font", clear_font)
	
	# 创建死亡消息
	death_message = Label.new()
	game_over_panel.add_child(death_message)
	death_message.text = "你的英雄已经倒下了...\n但这并不是结束！"
	death_message.position = Vector2(20, 70)
	death_message.size = Vector2(260, 50)
	death_message.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	death_message.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
	death_message.add_theme_color_override("font_color", Color.WHITE)
	death_message.add_theme_color_override("font_shadow_color", Color.BLACK)
	death_message.add_theme_constant_override("shadow_offset_x", 1)
	death_message.add_theme_constant_override("shadow_offset_y", 1)
	death_message.add_theme_font_size_override("font_size", 12)
	death_message.add_theme_font_override("font", clear_font)
	
	# 创建重新开始按钮
	restart_button = Button.new()
	game_over_panel.add_child(restart_button)
	restart_button.text = "重新开始"
	restart_button.position = Vector2(50, 140)
	restart_button.size = Vector2(80, 30)
	restart_button.add_theme_color_override("font_color", Color.WHITE)
	restart_button.add_theme_color_override("font_shadow_color", Color.BLACK)
	restart_button.add_theme_font_size_override("font_size", 12)
	restart_button.add_theme_font_override("font", clear_font)
	restart_button.pressed.connect(_on_restart_button_pressed)
	print("🔧 重新开始按钮创建完成，位置: ", restart_button.position, " 尺寸: ", restart_button.size)
	
	# 创建返回主菜单按钮
	main_menu_button = Button.new()
	game_over_panel.add_child(main_menu_button)
	main_menu_button.text = "返回主菜单"
	main_menu_button.position = Vector2(170, 140)
	main_menu_button.size = Vector2(80, 30)
	main_menu_button.add_theme_color_override("font_color", Color.WHITE)
	main_menu_button.add_theme_color_override("font_shadow_color", Color.BLACK)
	main_menu_button.add_theme_font_size_override("font_size", 12)
	main_menu_button.add_theme_font_override("font", clear_font)
	main_menu_button.pressed.connect(_on_main_menu_button_pressed)
	print("🔧 返回主菜单按钮创建完成，位置: ", main_menu_button.position, " 尺寸: ", main_menu_button.size)
	
	print("游戏结束界面创建完成")

func create_victory_overlay():
	print("🔧 开始创建通关界面...")
	
	# 如果已经存在，先清理
	if victory_overlay:
		print("⚠️ 通关界面已存在，先清理...")
		victory_overlay.queue_free()
	
	# 创建通关界面覆盖层
	victory_overlay = Control.new()
	add_child(victory_overlay)
	victory_overlay.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	victory_overlay.visible = false
	victory_overlay.mouse_filter = Control.MOUSE_FILTER_STOP  # 阻止点击穿透
	victory_overlay.process_mode = Node.PROCESS_MODE_ALWAYS
	
	# 创建金色背景（胜利主题）
	var background = ColorRect.new()
	victory_overlay.add_child(background)
	background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	background.color = Color(0.8, 0.6, 0.2, 0.7)  # 金色半透明
	
	# 创建通关面板
	victory_panel = Panel.new()
	victory_overlay.add_child(victory_panel)
	victory_panel.size = Vector2(350, 220)
	victory_panel.position = Vector2((get_viewport().get_visible_rect().size.x - 350) / 2, 
									(get_viewport().get_visible_rect().size.y - 220) / 2)
	victory_panel.add_theme_color_override("bg_color", Color(0.1, 0.1, 0.1, 0.95))
	
	# 创建抗锯齿字体
	var clear_font = SystemFont.new()
	clear_font.font_names = ["Arial", "Microsoft YaHei", "SimSun"]
	clear_font.antialiasing = TextServer.FONT_ANTIALIASING_GRAY
	clear_font.hinting = TextServer.HINTING_LIGHT
	
	# 创建通关标题
	victory_label = Label.new()
	victory_panel.add_child(victory_label)
	victory_label.text = "🏆 恭喜通关！ 🏆"
	victory_label.position = Vector2(20, 20)
	victory_label.size = Vector2(310, 40)
	victory_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	victory_label.add_theme_color_override("font_color", Color.GOLD)
	victory_label.add_theme_color_override("font_shadow_color", Color.BLACK)
	victory_label.add_theme_constant_override("shadow_offset_x", 2)
	victory_label.add_theme_constant_override("shadow_offset_y", 2)
	victory_label.add_theme_font_size_override("font_size", 22)
	victory_label.add_theme_font_override("font", clear_font)
	
	# 创建通关消息
	victory_message = Label.new()
	victory_panel.add_child(victory_message)
	victory_message.text = "你成功击败了所有敌人！\n这片地牢已经被你清理干净了！\n\n真是一场精彩的冒险！"
	victory_message.position = Vector2(20, 70)
	victory_message.size = Vector2(310, 80)
	victory_message.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	victory_message.vertical_alignment = VERTICAL_ALIGNMENT_CENTER
	victory_message.add_theme_color_override("font_color", Color.WHITE)
	victory_message.add_theme_color_override("font_shadow_color", Color.BLACK)
	victory_message.add_theme_constant_override("shadow_offset_x", 1)
	victory_message.add_theme_constant_override("shadow_offset_y", 1)
	victory_message.add_theme_font_size_override("font_size", 12)
	victory_message.add_theme_font_override("font", clear_font)
	
	# 创建重新开始按钮
	victory_restart_button = Button.new()
	victory_panel.add_child(victory_restart_button)
	victory_restart_button.text = "再来一局"
	victory_restart_button.position = Vector2(70, 170)
	victory_restart_button.size = Vector2(90, 35)
	victory_restart_button.add_theme_color_override("font_color", Color.WHITE)
	victory_restart_button.add_theme_color_override("font_shadow_color", Color.BLACK)
	victory_restart_button.add_theme_font_size_override("font_size", 13)
	victory_restart_button.add_theme_font_override("font", clear_font)
	victory_restart_button.pressed.connect(_on_victory_restart_button_pressed)
	print("🔧 通关重新开始按钮创建完成，位置: ", victory_restart_button.position, " 尺寸: ", victory_restart_button.size)
	
	# 创建返回主菜单按钮
	victory_main_menu_button = Button.new()
	victory_panel.add_child(victory_main_menu_button)
	victory_main_menu_button.text = "返回主菜单"
	victory_main_menu_button.position = Vector2(190, 170)
	victory_main_menu_button.size = Vector2(90, 35)
	victory_main_menu_button.add_theme_color_override("font_color", Color.WHITE)
	victory_main_menu_button.add_theme_color_override("font_shadow_color", Color.BLACK)
	victory_main_menu_button.add_theme_font_size_override("font_size", 13)
	victory_main_menu_button.add_theme_font_override("font", clear_font)
	victory_main_menu_button.pressed.connect(_on_victory_main_menu_button_pressed)
	print("🔧 通关返回主菜单按钮创建完成，位置: ", victory_main_menu_button.position, " 尺寸: ", victory_main_menu_button.size)
	
	print("通关界面创建完成")

func toggle_pause():
	if current_state != GameState.PLAYING and current_state != GameState.PAUSED:
		print("⚠️ 无法切换暂停状态，当前状态: ", current_state)
		return
		
	game_paused = !game_paused
	print("🎮 切换暂停状态: ", game_paused)
	
	if game_paused:
		current_state = GameState.PAUSED
		show_pause_overlay()
		pause_game_requested.emit()
		print("📤 发出暂停信号")
	else:
		current_state = GameState.PLAYING
		hide_pause_overlay()
		resume_game_requested.emit()
		print("📤 发出恢复信号")
	
	# 更新暂停按钮状态
	if game_controls:
		game_controls.set_pause_button_state(game_paused)

func show_pause_overlay():
	if pause_overlay:
		pause_overlay.visible = true
		print("显示暂停界面")

func hide_pause_overlay():
	if pause_overlay:
		pause_overlay.visible = false
		print("隐藏暂停界面")

func show_game_over():
	print("💀 显示游戏结束界面")
	current_state = GameState.GAME_OVER
	
	# 确保暂停界面完全隐藏
	hide_pause_overlay()
	game_paused = false
	
	# 隐藏其他UI元素
	if item_inventory:
		item_inventory.visible = false
		inventory_open = false
	
	# 显示游戏结束界面
	if game_over_overlay:
		game_over_overlay.visible = true
		print("✅ 游戏结束界面已显示")
		print("🔧 暂停界面状态: ", pause_overlay.visible if pause_overlay else "null")
		print("🔧 游戏结束界面状态: ", game_over_overlay.visible)

func hide_game_over():
	if game_over_overlay:
		game_over_overlay.visible = false
		print("隐藏游戏结束界面")

func show_victory():
	print("🏆 显示通关界面")
	current_state = GameState.VICTORY
	
	# 确保其他界面完全隐藏
	hide_pause_overlay()
	hide_game_over()
	game_paused = false
	
	# 隐藏其他UI元素
	if item_inventory:
		item_inventory.visible = false
		inventory_open = false
	
	# 显示通关界面
	if victory_overlay:
		victory_overlay.visible = true
		print("✅ 通关界面已显示")
		print("🔧 暂停界面状态: ", pause_overlay.visible if pause_overlay else "null")
		print("🔧 游戏结束界面状态: ", game_over_overlay.visible if game_over_overlay else "null")
		print("🔧 通关界面状态: ", victory_overlay.visible)

func hide_victory():
	if victory_overlay:
		victory_overlay.visible = false
		print("隐藏通关界面")

func _on_restart_button_pressed():
	print("🔄 重新开始按钮被点击")
	print("🔧 当前HUD状态: ", current_state)
	restart_game_requested.emit()
	print("📤 发出restart_game_requested信号")
	hide_game_over()

func _on_main_menu_button_pressed():
	print("🏠 返回主菜单按钮被点击")
	print("🔧 当前HUD状态: ", current_state)
	return_to_main_menu_requested.emit()
	print("📤 发出return_to_main_menu_requested信号")
	hide_game_over()

func toggle_inventory():
	if current_state != GameState.PLAYING:
		return
		
	inventory_open = !inventory_open
	if item_inventory:
		item_inventory.visible = inventory_open

func update_player_health(current_health: int, max_health: int):
	if health_display and current_state == GameState.PLAYING:
		health_display.update_health(current_health, max_health)

func update_player_stats(level: int, experience: int, coins: int):
	if player_stats and current_state == GameState.PLAYING:
		player_stats.update_stats(level, experience, coins)

func add_item_to_inventory(item_data: Dictionary):
	if item_inventory and current_state == GameState.PLAYING:
		item_inventory.add_item(item_data)

func _on_start_game_pressed():
	print("📥 收到开始游戏信号")
	start_game()

func _on_pause_pressed():
	print("📥 收到暂停按钮按下信号")
	toggle_pause()

func _on_menu_pressed():
	menu_requested.emit()

func _on_quit_game_pressed():
	print("📥 收到退出游戏信号")
	quit_game_requested.emit()

func _on_pause_main_menu_button_pressed():
	print("🏠 暂停界面返回主菜单按钮被点击")
	print("🔧 当前HUD状态: ", current_state)
	print("🔧 暂停界面可见: ", pause_overlay.visible if pause_overlay else "null")
	print("🔧 游戏结束界面可见: ", game_over_overlay.visible if game_over_overlay else "null")
	
	# 先隐藏暂停界面
	hide_pause_overlay()
	# 重置暂停相关状态
	game_paused = false
	current_state = GameState.START_SCREEN
	# 更新暂停按钮状态
	if game_controls:
		game_controls.set_pause_button_state(false)
	# 发出返回主菜单信号
	return_to_main_menu_requested.emit()
	print("📤 发出return_to_main_menu_requested信号")

func _on_victory_restart_button_pressed():
	print("🔄 通关界面重新开始按钮被点击")
	print("🔧 当前HUD状态: ", current_state)
	restart_game_requested.emit()
	print("📤 发出restart_game_requested信号")
	hide_victory()

func _on_victory_main_menu_button_pressed():
	print("🏠 通关界面返回主菜单按钮被点击")
	print("🔧 当前HUD状态: ", current_state)
	return_to_main_menu_requested.emit()
	print("📤 发出return_to_main_menu_requested信号")
	hide_victory()

func show_damage_number(position: Vector2, damage: int, color: Color = Color.RED):
	# 只在游戏进行中显示伤害数字
	if current_state != GameState.PLAYING:
		return
		
	# 显示伤害数字的浮动效果
	var damage_label = Label.new()
	add_child(damage_label)
	damage_label.text = str(damage)
	damage_label.modulate = color
	damage_label.position = position
	damage_label.add_theme_font_size_override("font_size", 16)  # 增大字体
	damage_label.add_theme_color_override("font_shadow_color", Color.BLACK)
	damage_label.add_theme_constant_override("shadow_offset_x", 1)
	damage_label.add_theme_constant_override("shadow_offset_y", 1)
	
	# 添加抗锯齿字体
	var damage_font = SystemFont.new()
	damage_font.font_names = ["Arial", "Microsoft YaHei", "SimSun"]
	damage_font.antialiasing = TextServer.FONT_ANTIALIASING_GRAY
	damage_font.hinting = TextServer.HINTING_LIGHT
	damage_label.add_theme_font_override("font", damage_font)
	
	# 动画效果
	var tween = create_tween()
	tween.parallel().tween_property(damage_label, "position", position + Vector2(0, -30), 1.0)
	tween.parallel().tween_property(damage_label, "modulate:a", 0.0, 1.0)
	tween.tween_callback(damage_label.queue_free)

# 重置游戏状态
func reset_game():
	set_start_screen_state()
	
	# 重置各组件状态
	game_paused = false
	inventory_open = false
	
	# 确保暂停界面被隐藏
	hide_pause_overlay()
	
	# 重置暂停按钮状态
	if game_controls:
		game_controls.set_pause_button_state(false) 
